﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.IO;
using System.Diagnostics;

namespace SwHelper.DocHelper
{
    class DocHelper : IDocHelper
    {
        private SldWorks swApp { get; set; }

        public DocHelper(SldWorks _swapp)
        {
            swApp = _swapp;
        }
        /// <summary>
        /// 打开单个模型文档
        /// </summary>
        /// <param name="docPath"></param>
        /// <returns></returns>
        public  ModelDoc2 OpenSingleDocument(string docPath, swOpenDocOptions_e swDocType)
        {
            ModelDoc2 modelDoc2 = null;
            if (File.Exists(docPath))
            {
                int intError=0, intWraning=0;
                modelDoc2 = swApp.OpenDoc6(docPath, (int)swDocType, (int)swOpenDocOptions_e.swOpenDocOptions_LoadModel, "", ref intError, ref intWraning);
            }
            return modelDoc2;
        }

        /// <summary>
        /// 修改尺寸参数 单位毫米
        /// </summary>
        /// <param name="paramsName"></param>
        /// <param name="_val"></param>
        public  void EditParameterByname(string paramsName, double _val)
        {
            ModelDoc2 modleDoc = swApp.ActiveDoc;
            int prefIntVal = modleDoc.Extension.GetUserPreferenceInteger((int)swUserPreferenceIntegerValue_e.swUnitSystem, 0);
            modleDoc.Extension.SetUserPreferenceInteger((int)swUserPreferenceIntegerValue_e.swUnitSystem, 0, (int)swUnitSystem_e.swUnitSystem_MMGS);
            modleDoc.Parameter(paramsName).SystemValue = _val;
            modleDoc.Extension.SetUserPreferenceInteger((int)swUserPreferenceIntegerValue_e.swUnitSystem, 0, prefIntVal);
            modleDoc.EditRebuild3();
        }


        public void AskDatums(ModelDoc2 modeldoc, out object listdatums)
        {
            listdatums = null;
            List<Feature> list = new List<Feature>();

            StringBuilder sb = new StringBuilder();
            sb.Append("RefPlane" + ",");
            sb.Append("OriginProfileFeature" + ",");
            sb.Append("RefAxis" + ",");

            FeatureManager featMgr = modeldoc.FeatureManager;
            object featObj = featMgr.GetFeatures(true);
            object[] feats = featObj as object[];

            foreach (object feat1 in feats)
            {
                Feature feat = feat1 as Feature;
                if(feat != null)
                {
                    string featname = feat.GetTypeName2();
                    if (sb.ToString().Contains(featname))
                        list.Add(feat);
                }
            }
            listdatums = list.ToArray();
        }

        public void AskFaces(PartDoc partdoc, out object faces)
        {
            faces = null;
            object[] bodys = partdoc.GetBodies2((int)swBodyType_e.swAllBodies, false);

            List<object> bodyfaces = new List<object>();
            foreach(Body2 body2 in bodys)
            {
                object[] singlebodyfaces = body2.GetFaces();
                bodyfaces.AddRange(singlebodyfaces);
            }
            faces = bodyfaces.ToArray();
        }
        public void AskEdges(PartDoc partdoc, out object obj)
        {
            obj = null;
            object[] bodys = partdoc.GetBodies2((int)swBodyType_e.swAllBodies, false);

            List<object> bodyfaces = new List<object>();
            foreach (Body2 body2 in bodys)
            {
                object[] singlebodyedges= body2.GetEdges();
                bodyfaces.AddRange(singlebodyedges);
            }
            obj = bodyfaces.ToArray();
        }
    }
}
